package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.BaseException;
import com.sky.mapper.admin.setMealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.SetmealDish;
import com.sky.result.PageResult;
import com.sky.service.admin.setMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description setMealServiceImpl
 * @Author luohai
 * @Date 2024-10-04
 */
@Service
@Transactional
public class setMealServiceImpl implements setMealService {
    @Autowired
    private setMealMapper setMealMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public void addSetMeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setMealMapper.addSetMeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setMealMapper.addsetmealDishes(setmealDishes);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult setMealPaginationQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page page = (Page) setMealMapper.setMealPaginationQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @Override
    public void deleteSetMealById(List<Long> ids) {
        for (Long id : ids) {
            Integer i = setMealMapper.selectStatusById(id);
            if (i == 1) {
                throw new BaseException("套餐启售无法删除！");
            }
        }
        setMealMapper.deleteSetMealById(ids);
        setMealMapper.deleteSetMealDishesById1(ids);
    }

    /**
     * 根据ID查询套餐（数据回显）
     *
     * @param setmealId
     * @return
     */
    @Override
    public SetmealVO selectSetMealById(Long setmealId) {
        return setMealMapper.selectSetMealById(setmealId);
    }

    /**
     * 根据ID修改套餐
     *
     * @param setmealDTO
     * @return
     */
    @Override
    public void modifySetMealById(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setMealMapper.modifySetMealById(setmeal);
        setMealMapper.deleteSetMealDishesById(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setMealMapper.addSetMealDishsById(setmealDishes);
    }

    /**
     * 起售停售套餐
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        List<Long> dishId = setMealMapper.searchDishId(id);
        List<Dish> list = setMealMapper.searchDishStatus(dishId);
        if (list != null && list.size() > 0) {
            for (Dish dish : list) {
                Integer status1 = dish.getStatus();
                if (status1 != 0) {
                    setMealMapper.startOrStop(status, id);
                } else {
                    throw new BaseException("菜品存在停售，起停售失败");
                }
            }
        }
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    public List<Setmeal> list(Long categoryId) {
        List<Setmeal> list = setMealMapper.list(categoryId);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }
}
